home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1991: Code Warrior / bincue / Code Warrior.bin / Developer Essentials / DTS Sample Code / System 7.0 Samples / CShell / Print.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-21  |  7.9 KB  |  308 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** MultiFinder-Aware Shell Application
  5. **
  6. ** Program:     CShell
  7. ** File:        print.c
  8. ** Written by:  Eric Soldan
  9. ** Based on:    Code from Pete "Luke" Alexander.
  10. **
  11. ** Copyright © 1989-1991 Apple Computer, Inc.
  12. ** All rights reserved.
  13. */
  14.  
  15.  
  16.  
  17. /*****************************************************************************/
  18.  
  19.  
  20.  
  21. #include "CShell.h"                /* Get the CShell includes/typedefs, etc.    */
  22. #include "CShellCommon.h"        /* Get the stuff in common with rez.        */
  23. #include "CShell.protos"        /* Get the prototypes for CShell.            */
  24.  
  25. #ifndef __ERRORS__
  26. #include <Errors.h>
  27. #endif
  28.  
  29. #ifndef __RESOURCES__
  30. #include <Resources.h>
  31. #endif
  32.  
  33.  
  34.  
  35. /*****************************************************************************/
  36.  
  37.  
  38.  
  39. static pascal void    PrintIdleProc(void);
  40.  
  41. short                gPrintPage;
  42. static DialogPtr    PrintingStatusDialog;
  43.  
  44.  
  45.  
  46. /*****************************************************************************/
  47.  
  48.  
  49.  
  50. /* This print-loop function is designed to be called under various situations.
  51. ** The big issue that it handles is finder printing.  If multiple documents
  52. ** are to be printed from the finder, the user should only see one job dialog
  53. ** for all the files.  (If a job dialog is shown for each file, how does the
  54. ** user know for which file the dialog is for?)  So, for situations where
  55. ** there is more than one file to be printed, call this code the first time
  56. ** with the firstJob boolean true.  Normally, the jobDlg boolean will also
  57. ** be true, except that under 7.0, you may be printing in the background.
  58. ** If this is the case, you don't want a job dialog for even the first file,
  59. ** and you should pass in false for the jobDlg boolean in this case.  For
  60. ** files 2-N, you should pass false for both booleans.  For regular application
  61. ** printing, you should pass true for both booleans, since the file is the
  62. ** first (only) file, and you are not in the background.
  63. **
  64. ** After calling this function to print a document, you need to call it
  65. ** again with a nil document handle.  The print record for the first (or only)
  66. ** document printed is preserved in a static variable.  This is so that the
  67. ** job dialog information can be passed on to documents 2-N in the print job.
  68. ** Calling this function with the document handle nil tells this function
  69. ** that you are done printing documents, and that the print record for the
  70. ** first job can be disposed of.
  71. */
  72.  
  73. #pragma segment Print
  74. OSErr    AppPrintDocument(FileRecHndl frHndl, Boolean jobDlg, Boolean firstJob)
  75. {
  76.     OSErr            err;
  77.     THPrint            prRecHndl;
  78.     TPPrPort        printPort;
  79.     GrafPtr            oldPort;
  80.     short            i, keepResFile, fstPage, lstPage, copies;
  81.     TPrStatus        status;
  82.     ControlHandle    proceedButton;
  83.     Rect             rct;
  84.  
  85.     static THPrint    prMergeHndl;
  86.  
  87.     if (!frHndl) {
  88.         if (prMergeHndl) {
  89.             DisposHandle((Handle)prMergeHndl);
  90.             prMergeHndl = nil;
  91.         }
  92.         return(noErr);
  93.     }
  94.  
  95.     PrintingStatusDialog = nil;
  96.  
  97.     if (!(prRecHndl = (THPrint)NewHandle(sizeof(TPrint)))) return(memFullErr);
  98.         /* If we can't generate a print record handle, we are out of here. */
  99.  
  100.     BlockMove((Ptr)&((*frHndl)->doc.print), (Ptr)(*prRecHndl), sizeof(TPrint));
  101.         /* Get the document's print info into the print record handle. */
  102.  
  103.     GetPort(&oldPort);
  104.  
  105.     DoSetCursor(&qd.arrow);
  106.     PrOpen();
  107.     err = PrError();
  108.  
  109.     if (!err) {
  110.         keepResFile = CurResFile();
  111.  
  112.         if (!(*frHndl)->doc.printRecValid) {
  113.             PrintDefault(prRecHndl);            /* The document print record was never 
  114.             err = PrError();                    ** initialized.  Now is is. */
  115.         }            
  116.         if (!err) {
  117.             PrValidate(prRecHndl);        /* Do this just 'cause Apple says so. */
  118.             err = PrError();
  119.         }
  120.         if (!err) {
  121.             if (jobDlg) {                /* User gets to click some buttons. */
  122.                 if (!(PrJobDialog(prRecHndl))) err = userCanceledErr;
  123.                 else                           err = PrError();
  124.             }
  125.         }
  126.         if (!err) {
  127.             if (!firstJob) {
  128.                 PrJobMerge(prMergeHndl, prRecHndl);
  129.                 err = PrError();
  130.             }
  131.         }
  132.  
  133.         if (!err) {            /* Put the defaulted/validated/jobDlg'ed print record in the doc. */
  134.             fstPage  = (*prRecHndl)->prJob.iFstPage;
  135.             lstPage  = (*prRecHndl)->prJob.iLstPage;
  136.             copies   = (*prRecHndl)->prJob.iCopies;
  137.             BlockMove((Ptr)(*prRecHndl), (Ptr)&((*frHndl)->doc.print), sizeof(TPrint));
  138.             (*frHndl)->doc.printRecValid = true;
  139.  
  140.             ParamText((*frHndl)->fileState.fss.name, nil, nil, nil);
  141.             PrintingStatusDialog = GetNewDialog(rPrStatusDlg, nil, (WindowPtr)-1);
  142.             if (PrintingStatusDialog) {
  143.                 GetDItem(PrintingStatusDialog, 1, &i, &(Handle)proceedButton, &rct);
  144.                 HiliteControl(proceedButton, 255);
  145.                     /* Setup the proceed/pause/cancel dialog with the document name. */
  146.                 (*prRecHndl)->prJob.pIdleProc = PrintIdleProc;
  147.                 UseResFile(keepResFile);
  148.                     /* Hook in the proceed/pause/cancel dialog. */
  149.             }
  150.  
  151.             for (i = 1; (i <= copies) && (!err); ++i) {
  152.  
  153.                 printPort = PrOpenDoc(prRecHndl, nil, nil);
  154.                 if (!(err = PrError())) {
  155.  
  156.                     gPrintPage = fstPage;
  157.                     while (gPrintPage <= lstPage) {
  158.  
  159.                         PrOpenPage(printPort, nil);
  160.  
  161.                         if (!(err = PrError()))
  162.                             ImageDocument(frHndl);
  163.                                 /* Do the print thing here. */
  164.  
  165.                         PrClosePage(printPort);
  166.  
  167.                         if (!gPrintPage) break;
  168.                         ++gPrintPage;
  169.                     }
  170.                     gPrintPage = 0;
  171.                     PrCloseDoc(printPort);
  172.                 }
  173.             }
  174.         }
  175.  
  176.         if (
  177.             (!err) &&
  178.             ((*prRecHndl)->prJob.bJDocLoop == bSpoolLoop) &&
  179.             (!(err = PrError()))
  180.         ) {
  181.             PrPicFile(prRecHndl, nil, nil, nil, &status);
  182.             err = PrError();
  183.         }
  184.     }
  185.  
  186.     if (firstJob) prMergeHndl = prRecHndl;
  187.     else          DisposHandle((Handle)prRecHndl);
  188.  
  189.     if (PrintingStatusDialog) DisposDialog(PrintingStatusDialog);
  190.  
  191.     PrClose();
  192.     SetPort(oldPort);
  193.  
  194.     return(err);
  195. }
  196.  
  197.  
  198.  
  199. /*****************************************************************************/
  200.  
  201.  
  202.  
  203. /* PrintIdleProc will handle events in the 'Printing Status Dialog' which
  204. ** gives the user the option to 'Proceed', 'Pause', or 'Cancel' the current
  205. ** printing job during print time.
  206. **
  207. ** The buttons:
  208. **        1: Proceed
  209. **        2: Pause
  210. **        3: Cancel 
  211. */
  212.  
  213. #pragma segment Print
  214. pascal void        PrintIdleProc(void)
  215. {
  216.     Boolean                button, paused;
  217.     ControlHandle        pauseButton, proceedButton;
  218.     DialogPtr            aDialog;
  219.     EventRecord            anEvent;
  220.     GrafPtr                oldPort;
  221.     Rect                 rct;
  222.     short                item, itemType, keepResFile;
  223.  
  224.     GetPort(&oldPort);
  225.  
  226.     UseResFile(keepResFile = CurResFile());
  227.  
  228.     GetDItem(PrintingStatusDialog, 1, &itemType, &(Handle)proceedButton, &rct);
  229.     HiliteControl(proceedButton, 255);
  230.     GetDItem(PrintingStatusDialog, 2, &itemType, &(Handle)pauseButton, &rct);
  231.  
  232.     paused = false;
  233.     do {
  234.         if (GetNextEvent((mDownMask + mUpMask + updateMask), &anEvent)) {
  235.             if (PrintingStatusDialog != FrontWindow ())
  236.             SelectWindow(PrintingStatusDialog);
  237.  
  238.             if (IsDialogEvent(&anEvent)) {
  239.                 button = DialogSelect(&anEvent, &aDialog, &item);
  240.  
  241.                 if ((button) && (aDialog == PrintingStatusDialog)) {
  242.                     switch (item) {
  243.                         case 1:
  244.                             HiliteControl(pauseButton, 0);        /* Enable PAUSE    */
  245.                             HiliteControl(proceedButton, 255);    /* Disable PROCEED */
  246.                             paused = false;
  247.                             break;
  248.                         case 2:
  249.                             HiliteControl(pauseButton, 255);    /* Disable PAUSE  */
  250.                             HiliteControl(proceedButton, 0);    /* Enable PROCEED */
  251.                             paused = true;
  252.                             break;
  253.                         case 3:
  254.                             PrSetError(iPrAbort);               /* CANCEL printing */
  255.                             paused = false;
  256.                             break;
  257.                     }
  258.                 }
  259.             }
  260.         }
  261.     } while (paused != false); 
  262.  
  263.     SetPort(oldPort);
  264. }
  265.  
  266.  
  267.  
  268. /*****************************************************************************/
  269.  
  270.  
  271.  
  272. #pragma segment Print
  273. OSErr    PresentStyleDialog(FileRecHndl frHndl)
  274. {
  275.     OSErr        err;
  276.     THPrint        prRecHndl;
  277.  
  278.     if (!(prRecHndl = (THPrint)NewHandle(sizeof(TPrint))))
  279.         return(memFullErr);
  280.  
  281.     PrOpen();
  282.  
  283.     if (!(err = PrError())) {
  284.  
  285.         BlockMove((Ptr)&(*frHndl)->doc.print, (Ptr)*prRecHndl, sizeof(TPrint));
  286.             /* Get data, valid or not. */
  287.  
  288.         if (!(*frHndl)->doc.printRecValid) PrintDefault(prRecHndl);
  289.         else                                PrValidate(prRecHndl);
  290.         if (!(err = PrError())) {
  291.             if (PrStlDialog(prRecHndl)) {
  292.                 BlockMove((Ptr)*prRecHndl, (Ptr)&(*frHndl)->doc.print, sizeof(TPrint));
  293.                 (*frHndl)->doc.printRecValid  = true;
  294.                 (*frHndl)->fileState.docDirty = true;
  295.             }
  296.             else err = userCanceledErr;
  297.         }
  298.     }
  299.  
  300.     DisposHandle((Handle)prRecHndl);
  301.     PrClose();
  302.  
  303.     return(err);
  304. }
  305.  
  306.  
  307.  
  308.